import { useEffect, useRef, useState } from 'react'
import { Form, message } from 'antd'
import { useRecoilValue } from 'recoil'
import { deleteWard, getTableList, updateWard } from '../service'
import { isNullArray, isNullData, isSuccess } from '@/utils'
import { WardHook, WordTree } from '../data'
import { userInfo } from '@/store/userInfo'

export default (): WardHook => {
  const user = useRecoilValue(userInfo)
  const wardInput = useRef<HTMLInputElement>(null)
  const [wardForm] = Form.useForm()
  const [wardList, setWardList] = useState<WordTree[]>([])
  // 总数
  const [wardTotal, setWardTotal] = useState<number>(0)

  // 一级分类索引
  const [seleteMenu, setSelectMenu] = useState<number>(0)
  // 二级分类索引
  const [seleteMenuTwo, setSelectMenuTwo] = useState<number>(0)

  const [isEdit, setIsEdit] = useState<boolean>(false)
  // 添加状态
  const [isAdd, setIsAdd] = useState<boolean>(false)

  // 获取数据
  const getTables = async () => {
    const params = {
      hospitalCode: user.hospitalCode,
    }
    const res = await getTableList(params)
    if (isNullData(res) && res.data && isNullArray(res.data)) {
      setWardList(res.data.endemicAreaVos)
      setWardTotal(res.data.total)
    } else {
      setWardList([])
      setWardTotal(0)
    }
  }

  // 添加病区
  const addWard = () => {
    setIsAdd(true)
  }

  // 取消
  const handleCancleWard = () => {
    setIsAdd(false)
    wardForm.resetFields()
  }

  // 调接口
  const saveWard = async () => {
    const res = await wardForm.validateFields()
    const fatherId = wardList![seleteMenu].endemicAreaVos![seleteMenuTwo].id
    if (res) {
      const respone = await updateWard({
        unit: res.unit,
        fatherId,
      })
      if (isNullData(respone) && respone.data && isSuccess(respone.data)) {
        message.success('添加成功')
        getTables()
        handleCancleWard()
      } else {
        message.error(respone.data?.message)
      }
    }
  }

  // 删除病区
  const deleteWarded = async (id: number) => {
    const res = await deleteWard({ id })
    if (isNullData(res) && res.data && isSuccess(res.data)) {
      message.success('删除成功')
      getTables()
    } else {
      message.error(res.data?.message)
    }
  }

  // 编辑
  const handleWardEdit = ({ id, unit }: { id: number; unit: string }) => {
    setIsEdit(true)
    const newlist = [...wardList]
    const info = newlist![seleteMenu].endemicAreaVos![seleteMenuTwo].endemicAreaVos?.find((item) => item.id === id)
    if (info) {
      info.flag = true
    }
    setWardList(newlist)
    wardForm.setFieldsValue({
      unit,
    })
    wardInput?.current!.focus()
  }

  // 编辑接口
  const editWard = async ({ id, code }: { id: number; code: number }) => {
    const res = await wardForm.validateFields()
    const fatherId = wardList![seleteMenu].endemicAreaVos![seleteMenuTwo].id
    if (res) {
      const respone = await updateWard({
        id,
        unit: res.unit,
        fatherId,
        code,
      })
      if (isNullData(respone) && respone.data && isSuccess(respone.data)) {
        message.success('编辑成功')
        wardForm.resetFields()
        getTables()
        setIsEdit(false)
        // handleCancleWard()
      } else {
        message.error(respone.data?.message)
      }
    }
  }

  const handleEditWardCancel = () => {
    getTables()
    setIsEdit(false)
    wardForm.resetFields()
  }

  useEffect(() => {
    if (isAdd) {
      wardInput?.current!.focus()
    }
  }, [isAdd])

  useEffect(() => {
    getTables()
  }, [])
  return {
    wardList,
    setSelectMenu,
    seleteMenu,
    setSelectMenuTwo,
    seleteMenuTwo,
    wardTotal,
    addWard,
    isAdd,
    wardForm,
    saveWard,
    handleCancleWard,
    deleteWarded,
    handleWardEdit,
    editWard,
    wardInput,
    isEdit,
    handleEditWardCancel,
  }
}
